{% extends "base.html" %}

{% block title %}搜索教练 - 乒乓球培训管理系统{% endblock %}

{% block extra_css %}
<style>
    .coach-card {
        transition: all 0.3s ease;
        border: 1px solid #e5e5e5;
        border-radius: 8px;
        background: white;
        margin-bottom: 20px;
    }

    .coach-card.has-relation {
        border: 2px solid #28a745;
        background: linear-gradient(to right, rgba(40, 167, 69, 0.05), rgba(255, 255, 255, 1));
    }

    .coach-card.pending-relation {
        border: 2px solid #ffc107;
        background: linear-gradient(to right, rgba(255, 193, 7, 0.05), rgba(255, 255, 255, 1));
    }

    .coach-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        border-color: #007bff;
    }

    .coach-avatar {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        object-fit: cover;
        border: 3px solid #fff;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }

    .coach-level-badge {
        font-size: 0.75rem;
        padding: 2px 8px;
        border-radius: 12px;
    }

    .level-senior { background: linear-gradient(45deg, #ffd700, #ff8c00); color: white; }
    .level-middle { background: linear-gradient(45deg, #c0c0c0, #808080); color: white; }
    .level-junior { background: linear-gradient(45deg, #cd7f32, #8b4513); color: white; }

    .search-filters {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
        color: white;
    }

    .search-filters .form-control,
    .search-filters .form-select {
        background: rgba(255,255,255,0.9);
        border: none;
        border-radius: 5px;
    }

    .stats-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-radius: 8px;
        padding: 15px;
        margin-bottom: 20px;
    }

    .stats-item {
        text-align: center;
        padding: 10px;
    }

    .stats-number {
        font-size: 1.5rem;
        font-weight: bold;
        display: block;
    }

    .stats-label {
        font-size: 0.8rem;
        opacity: 0.9;
    }

    .coach-info h6 {
        color: #333;
        margin-bottom: 5px;
    }

    .coach-meta {
        color: #666;
        font-size: 0.9rem;
    }

    .hourly-rate {
        color: #28a745;
        font-weight: bold;
        font-size: 1.1rem;
    }

    .btn-apply {
        background: linear-gradient(45deg, #28a745, #20c997);
        border: none;
        border-radius: 20px;
        padding: 8px 20px;
        color: white;
        font-size: 0.9rem;
    }

    .btn-apply:hover {
        background: linear-gradient(45deg, #218838, #1ea085);
        color: white;
    }

    .btn-view {
        background: linear-gradient(45deg, #007bff, #0056b3);
        border: none;
        border-radius: 20px;
        padding: 8px 20px;
        color: white;
        font-size: 0.9rem;
    }

    .btn-view:hover {
        background: linear-gradient(45deg, #0056b3, #004085);
        color: white;
    }

    .empty-state {
        text-align: center;
        padding: 60px 20px;
        color: #666;
    }

    .empty-state i {
        font-size: 4rem;
        color: #ddd;
        margin-bottom: 20px;
    }

    .relation-badge {
        font-size: 0.75rem;
        padding: 3px 8px;
        border-radius: 12px;
        margin-left: 5px;
    }

    .relation-approved {
        background: #28a745;
        color: white;
    }

    .relation-pending {
        background: #ffc107;
        color: #333;
    }

    .relation-rejected {
        background: #dc3545;
        color: white;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2>
                    <i class="fas fa-search me-2"></i>搜索教练
                </h2>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ url_for('dashboard.index') }}">首页</a></li>
                        <li class="breadcrumb-item active">搜索教练</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>

    <!-- 搜索筛选区域 -->
    <div class="row">
        <div class="col-12">
            <div class="search-filters">
                <form method="GET" action="{{ url_for('student_coach.search') }}" id="searchForm">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="form-label fw-bold">关键词搜索</label>
                            <input type="text" class="form-control" name="keyword"
                                   value="{{ keyword }}" placeholder="教练姓名、简介、经验">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label fw-bold">性别</label>
                            <select class="form-select" name="gender">
                                <option value="">不限</option>
                                <option value="male" {{ 'selected' if gender == 'male' }}>男</option>
                                <option value="female" {{ 'selected' if gender == 'female' }}>女</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label fw-bold">教练等级</label>
                            <select class="form-select" name="coach_level">
                                <option value="">不限</option>
                                <option value="junior" {{ 'selected' if coach_level == 'junior' }}>初级</option>
                                <option value="middle" {{ 'selected' if coach_level == 'middle' }}>中级</option>
                                <option value="senior" {{ 'selected' if coach_level == 'senior' }}>高级</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label fw-bold">排序方式</label>
                            <select class="form-select" name="sort_by">
                                <option value="rating_desc" {{ 'selected' if sort_by == 'rating_desc' }}>评分从高到低</option>
                                <option value="rating_asc" {{ 'selected' if sort_by == 'rating_asc' }}>评分从低到高</option>
                                <option value="rate_asc" {{ 'selected' if sort_by == 'rate_asc' }}>价格从低到高</option>
                                <option value="rate_desc" {{ 'selected' if sort_by == 'rate_desc' }}>价格从高到低</option>
                                <option value="experience_desc" {{ 'selected' if sort_by == 'experience_desc' }}>经验丰富</option>
                                <option value="name_asc" {{ 'selected' if sort_by == 'name_asc' }}>姓名排序</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">&nbsp;</label>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-light fw-bold">
                                    <i class="fas fa-search me-1"></i>搜索
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 统计信息 -->
    <div class="row">
        <div class="col-12">
            <div class="stats-card">
                <div class="row">
                    <div class="col-md-2 stats-item">
                        <span class="stats-number">{{ summary.total_coaches }}</span>
                        <span class="stats-label">总教练</span>
                    </div>
                    <div class="col-md-2 stats-item">
                        <span class="stats-number">{{ summary.available_coaches }}</span>
                        <span class="stats-label">可选教练</span>
                    </div>
                    <div class="col-md-2 stats-item">
                        <span class="stats-number">{{ summary.senior_coaches }}</span>
                        <span class="stats-label">高级教练</span>
                    </div>
                    <div class="col-md-2 stats-item">
                        <span class="stats-number">{{ summary.middle_coaches }}</span>
                        <span class="stats-label">中级教练</span>
                    </div>
                    <div class="col-md-2 stats-item">
                        <span class="stats-number">{{ summary.male_coaches }}</span>
                        <span class="stats-label">男教练</span>
                    </div>
                    <div class="col-md-2 stats-item">
                        <span class="stats-number">￥{{ "%.0f"|format(summary.avg_hourly_rate|float) }}</span>
                        <span class="stats-label">平均课时费</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 搜索结果 -->
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0">
                    <i class="fas fa-list me-2"></i>搜索结果
                    <span class="badge bg-primary">共 {{ total }} 位教练</span>
                </h5>
                <div class="btn-group" role="group">
                    <a href="{{ url_for('student_coach.search') }}" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-th me-1"></i>卡片视图
                    </a>
                    <a href="{{ url_for('student_coach.list') }}" class="btn btn-outline-secondary btn-sm">
                        <i class="fas fa-list me-1"></i>列表视图
                    </a>
                </div>
            </div>
        </div>
    </div>

    {% if coaches %}
    <div class="row">
        {% for coach in coaches %}
        <div class="col-lg-6 col-xl-4">
            <div class="coach-card p-3 {% if coach.relation_status == 'approved' %}has-relation{% elif coach.relation_status == 'pending' %}pending-relation{% endif %}">
                <div class="d-flex align-items-start">
                    <div class="flex-shrink-0 me-3">
                        <img src="{{ coach.photo_url or url_for('static', filename='images/default-avatar.svg') }}"
                             alt="{{ coach.real_name }}" class="coach-avatar">
                    </div>
                    <div class="flex-grow-1 coach-info">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <div>
                                <h6 class="mb-0 d-inline">{{ coach.real_name }}</h6>
                                {% if coach.relation_status == 'approved' %}
                                <span class="relation-badge relation-approved">
                                    <i class="fas fa-check-circle me-1"></i>已是我的教练
                                </span>
                                {% elif coach.relation_status == 'pending' %}
                                <span class="relation-badge relation-pending">
                                    <i class="fas fa-clock me-1"></i>申请审核中
                                </span>
                                {% elif coach.relation_status == 'rejected' %}
                                <span class="relation-badge relation-rejected">
                                    <i class="fas fa-times-circle me-1"></i>申请被拒绝
                                </span>
                                {% endif %}
                            </div>
                            <span class="coach-level-badge level-{{ coach.coach_level }}">
                                {% if coach.coach_level == 'senior' %}高级{% elif coach.coach_level == 'middle' %}中级{% else %}初级{% endif %}
                            </span>
                        </div>

                        <div class="coach-meta mb-2">
                            <span class="me-3">
                                <i class="fas fa-venus-mars me-1"></i>
                                {{ '男' if coach.gender == 'male' else '女' }}
                            </span>
                            {% if coach.age %}
                            <span class="me-3">
                                <i class="fas fa-birthday-cake me-1"></i>{{ coach.age }}岁
                            </span>
                            {% endif %}
                            <span>
                                <i class="fas fa-users me-1"></i>{{ coach.student_relations.filter_by(status='approved').count() }}名学员
                            </span>
                        </div>

                        {% if coach.bio %}
                        <p class="mb-2 text-muted" style="font-size: 0.9rem;">
                            {{ coach.bio[:60] }}{% if coach.bio|length > 60 %}...{% endif %}
                        </p>
                        {% endif %}

                        <div class="d-flex justify-content-between align-items-center">
                            <div class="hourly-rate">
                                ￥{{ coach.hourly_rate or 0 }}/课时
                            </div>
                            <div class="btn-group btn-group-sm" role="group">
                                <a href="{{ url_for('student_coach.detail', coach_id=coach.id) }}"
                                   class="btn btn-view">
                                    <i class="fas fa-eye me-1"></i>详情
                                </a>
                                {% if coach.relation_status == 'approved' %}
                                <button class="btn btn-secondary" disabled>
                                    <i class="fas fa-check me-1"></i>已选择
                                </button>
                                {% elif coach.relation_status == 'pending' %}
                                <button class="btn btn-warning" disabled>
                                    <i class="fas fa-clock me-1"></i>审核中
                                </button>
                                {% else %}
                                <a href="{{ url_for('student_coach.apply', coach_id=coach.id) }}"
                                   class="btn btn-apply">
                                    <i class="fas fa-user-plus me-1"></i>申请
                                </a>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- 分页 -->
    {% if pagination and pagination.pages > 1 %}
    <div class="row">
        <div class="col-12">
            <nav aria-label="教练列表分页">
                <ul class="pagination justify-content-center">
                    {% if pagination.has_prev %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('student_coach.search', page=pagination.prev_num,
                                                             keyword=keyword, gender=gender, coach_level=coach_level, sort_by=sort_by) }}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% endif %}

                    {% for page_num in pagination.iter_pages() %}
                        {% if page_num %}
                            {% if page_num != pagination.page %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('student_coach.search', page=page_num,
                                                                     keyword=keyword, gender=gender, coach_level=coach_level, sort_by=sort_by) }}">
                                    {{ page_num }}
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item active">
                                <span class="page-link">{{ page_num }}</span>
                            </li>
                            {% endif %}
                        {% else %}
                        <li class="page-item disabled">
                            <span class="page-link">…</span>
                        </li>
                        {% endif %}
                    {% endfor %}

                    {% if pagination.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('student_coach.search', page=pagination.next_num,
                                                             keyword=keyword, gender=gender, coach_level=coach_level, sort_by=sort_by) }}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
    </div>
    {% endif %}

    {% else %}
    <!-- 空状态 -->
    <div class="row">
        <div class="col-12">
            <div class="empty-state">
                <i class="fas fa-search"></i>
                <h4>未找到符合条件的教练</h4>
                <p class="text-muted">请调整搜索条件或筛选选项后重试</p>
                <a href="{{ url_for('student_coach.search') }}" class="btn btn-primary">
                    <i class="fas fa-refresh me-1"></i>重新搜索
                </a>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 搜索表单自动提交
    $('#searchForm select').change(function() {
        $('#searchForm').submit();
    });

    // 实时搜索（延迟提交）
    let searchTimeout;
    $('#searchForm input[name="keyword"]').on('input', function() {
        clearTimeout(searchTimeout);
        searchTimeout = setTimeout(function() {
            $('#searchForm').submit();
        }, 500);
    });

    // 卡片悬停效果
    $('.coach-card').hover(
        function() {
            $(this).find('.btn-group').addClass('show');
        },
        function() {
            $(this).find('.btn-group').removeClass('show');
        }
    );

    // 加载统计信息
    loadCoachSummary();
});

function loadCoachSummary() {
    $.get('{{ url_for("student_coach.api_coach_summary") }}')
        .done(function(response) {
            if (response.success) {
                const data = response.data;
                // 更新统计数字（如果需要实时更新）
                console.log('教练统计数据已加载:', data);
            }
        })
        .fail(function() {
            console.log('加载统计数据失败');
        });
}
</script>
{% endblock %}